﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Administracja
{
    public partial class Glowne : Form
    {
        private string _bazaNazwa = "";

        public Glowne(int open)
        {
            InitializeComponent();

            Clean();
            Wyswietl();
            try
            {
                budunek.SelectedIndex = 0;
            }
            catch { }
            AktualizujConnectionString();

            if (open == 1) Karty_Click(this, EventArgs.Empty);
            if (open == 2) Zakupy_Click(this, EventArgs.Empty);
        }

        private void Wyswietl()
        {
            budunek.Items.Clear();

            var reader2D = Baza.Wczytaj2DData("SELECT Nazwa, Pokaz FROM DATA ORDER BY Nazwa DESC");

            foreach (var reader1D in reader2D)
            {
                if (reader1D[1] == "T")
                {
                    budunek.Items.Add(reader1D[0]);
                }
            }
        }

        private void AktualizujConnectionString()
        {
            var sciezka = Baza.Wczytaj2DData("SELECT Sciezka FROM DATA WHERE NAZWA = '" + _bazaNazwa + "'")[0][0];

            Baza.CloseConnection();

            Baza.ConnectionString = sciezka;

            Baza.OpenConnection();
        }

        public void Clean()
        {
            panel.Controls.Clear();
            Karty.BackColor = Color.ForestGreen;
            Kartoteki.BackColor = Color.ForestGreen;
            Zakupy.BackColor = Color.ForestGreen;
            Inne.BackColor = Color.ForestGreen;
        }

        private void Karty_Click(object sender, EventArgs e)
        {
            Clean();
            Karty.BackColor = Color.Transparent;
            panel.Controls.Add(new Osoby());
        }

        private void Zakupy_Click(object sender, EventArgs e)
        {
            Clean();
            Zakupy.BackColor = Color.Transparent;
            panel.Controls.Add(new Zakupy());
        }

        private void Kartoteki_Click(object sender, EventArgs e)
        {
            Clean();
            Kartoteki.BackColor = Color.Transparent;
            panel.Controls.Add(new Wydruki());
        }

        private void Inne_Click(object sender, EventArgs e)
        {
            Clean();
            Inne.BackColor = Color.Transparent;
            panel.Controls.Add(new Inne());
        }

        private void zamknij_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void budunek_SelectedIndexChanged(object sender, EventArgs e)
        {
            Clean();

            _bazaNazwa = budunek.Text;

            AktualizujConnectionString();
        }

        private void budunek_MouseClick(object sender, MouseEventArgs e)
        {
            Wyswietl();
            AktualizujConnectionString();
        }

        private void wroc_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
